package com.mhuang.module.wechat.service.impl;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.mhuang.module.wechat.config.WechatConfig;
import com.mhuang.module.wechat.service.WechatService;
import com.mhuang.wechat.common.model.message.ArticleResMessage;
import com.mhuang.wechat.common.model.message.TextResMessage;
import com.mhuang.wechat.common.pool.WechatExecutor;
import com.mhuang.wechat.common.pool.service.ExecuteService;
import com.mhuang.wechat.common.service.CoreService;
import com.mhuang.wechat.common.utils.MessageUtils;

/**
 * 
 * @Package: com.mhuang.module.wechat.service.impl
 * @Description 微信核心处理类
 * @author huang.miao
 * @date 2017年2月8日 下午4:35:21  
 * @since 1.0.0
 * @group skiper-opensource
 */
@Service("wechatService")
public class WechatServiceImpl extends CoreService implements WechatService{

	public Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private WechatExecutor wechatExecutor;
	
	@Autowired
	private ExecuteService executeService;
	
	@Override
	public String callback(Map<String, String> paramsMap) {
		try {
			return manager(paramsMap);
		} catch (Exception e) {
			logger.error("微信处理异常",e);
		}
		return null;
	}

	@Override
	public String subscribe(String openId, String appId) {
		logger.info("关注OpenId:"+appId);
		wechatExecutor.subscribe(openId, WECHAT_SUBSCRIBE, executeService);
		TextResMessage textMessage = new TextResMessage(openId,appId);
		textMessage.setContent("欢迎关注E学车微信号！");
		MessageUtils<TextResMessage> messageUtils = new MessageUtils<TextResMessage>();
		return messageUtils.fromObjectToXml(textMessage);
	}

	@Override
	public String unSubscribe(String openId, String appId) {
		wechatExecutor.subscribe(openId, WECHAT_UNSUBSCRIBE,executeService);
		return null;
	}

	@Override
	public String click(String openId, String appId, String eventKey) {
		return null;
	}

	@Override
	public String view(String openId, String appId, String eventKey) {
		return null;
	}

	@Override
	public String textMsg(String openId, String appId, String msg) {
		logger.info("文本消息"+msg);
		TextResMessage textMessage = new TextResMessage(openId,appId);
		textMessage.setContent("你发送的文本消息");
		MessageUtils<TextResMessage> messageUtils = new MessageUtils<TextResMessage>();
		return messageUtils.fromObjectToXml(textMessage);
	}

	@Override
	public String imageMsg(String openId, String appId) {
		return null;
	}

	
	/**
	 * 关注带二维码事件
	 * eventKey跟scan直接有点不一样。带有qrscene_、需要手动替换后根据你自己的业务进行处理
	 * eventKey 也是你生成的二维码哈
	 */
    @Override
    public String subscribe(String openId, String appId, String eventKey) {
        logger.info("关注OpenId:"+appId+":带eventKey:"+eventKey);
        
        //关注成功采用线程池处理。你可根据自己的业务指定使用spring还是jdk、jwechat都提供的有
        wechatExecutor.subscribeEventOther(openId, WECHAT_SUBSCRIBE,eventKey, executeService);
        eventKey = eventKey.replace("qrscene_", "");
    	TextResMessage textMessage = new TextResMessage(openId,appId);
        textMessage.setContent("Hi，感谢你扫描XX人二维码关注");
        MessageUtils<TextResMessage> messageUtils = new MessageUtils<TextResMessage>();
        return messageUtils.fromObjectToXml(textMessage);
    }
    public static void main(String[] args) {
    	ArticleResMessage news = new ArticleResMessage("1", "2");
		news.setCreateTime(System.currentTimeMillis());

		news.addArticle("TITLE", "description", "poster_url", "url");
		MessageUtils<ArticleResMessage> messageUtils = new MessageUtils<ArticleResMessage>();
		System.out.println(messageUtils.fromObjectToXml(news));
	}

    /**
     * eventKey代表你扫描的QRCODE
     */
	@Override
	public String scan(String openId, String appId, String eventKey) {
    	// 响应图文消息测试
		ArticleResMessage news = new ArticleResMessage(openId, appId);
		news.setCreateTime(System.currentTimeMillis());

		news.addArticle("标题", "说明", "头像","点击的URL");
		MessageUtils<ArticleResMessage> messageUtils = new MessageUtils<ArticleResMessage>();
		return messageUtils.fromObjectToXml(news);
	}
}
